<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/20
 * Time: 9:43
 */

namespace app\alliance\controller;


use app\service\RedPacketService;
use app\service\ScrollborderService;
use app\service\TaskService;
use Redis\Redis;
use think\App;
use think\Db;

class Home extends Common
{
    private $_Redis;
    function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->_Redis=new Redis();
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 首页签到提示、任务提示、系统通知提示 红包提示 轮播提示
     */
    function showInfo()
    {
        # 查询该用户是否有未完成的签到
        $today_sign_status=$this->_Redis->hGetJson("memberIsSign",$this->code);
        if(empty($today_sign_status)){
            $sign=Db::table("log_member_sign")->where(array("member_code"=>$this->code))->where("date(create_time)='".date("Y-m-d")."'")->find();
            if(!empty($sign)){
                $today_sign_status=true;
            }else{
                $today_sign_status=false;
            }
        }else{
            $today_sign_status=$today_sign_status==1 ? false :true;
        }
        # 查询用户是否有未完成的任务
        $task_service=new TaskService($this->memberInfo['code'],$this->memberInfo['level']);
        $task_service->taskInit();
        $task_count=$task_service->taskCount()>0?true:false;

        # 系统通知消息显示
        $PlatForm=$this->showPlatForm($this->memberInfo['type'],$this->memberInfo["level"]);

        # 查询用户团队是否有未领取过的红包
        $redpacket=$this->showRedPacket();

        # 查询滚动轮播
        $scroll=$this->showScroll();

        return array("status"=>true,"today_sign_status"=>$today_sign_status,"task_count"=>$task_count,"platform"=>$PlatForm,"redpacket"=>$redpacket,
            "scroll"=>$scroll);
    }

    /**
     * @param $_member_type 用户身份是不是合伙人
     * @param $_member_level 用户会员等级
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context
     */
    function showPlatForm($_member_type,$_member_level)
    {
        $status=false;
        $_list=$this->_Redis->hGetJson('showPlatForm',"showPlatForm");
        if(!empty($_list)){
            if($_list['pop_status']){
                if($_list['send_time']<=date("Y-m-d H:i:s")){
                    $status=true;
                }
                if($_list['people_type']==2 and $_member_type==2){
                    $status=true;
                }
                if($_list['people_type']==3 and !empty($_list['member_level'])){
                    $_level=explode(',',$_list['member_level']);
                    if(in_array($_member_level,$_level)){
                        $status=true;
                    }
                }
                return array("status"=>$status,"data"=>$_list);
            }else{
                return array("status"=>$status);
            }
        }else{
            # 查询当前是否有弹窗
            $_pop=Db::table("notice nt")
                ->field("nt.*,concat(',',nl.member_level) member_level")
                ->join("notice_level nl","nl.notice_id=nt.id and nt.people_type=3","left")
                ->where(array("nt.status"=>1,"nt.type"=>2))
                ->where("date(nt.send_time)<='".DATESTAMP."' and nt.stop_time>='".date("Y-m-d H:i:s")."'")
                ->find();
            if(!empty($_pop)){
                # 计算结束时间距离现在多少秒
                $_exec_time=strtotime($_pop['stop_time'])-time();
                $_data_array=array("pop_status"=>true,"title"=>$_pop['title'],"content"=>$_pop['content'],
                    "send_time"=>$_pop['send_time'],"send_name"=>$_pop['send_time'],"people_type"=>$_pop['people_type'],"stop_time"=>$_pop['stop_time'],
                    "is_see"=>$_pop['is_see'],"allow_status"=>$_pop['allow_status'],"member_level"=>$_pop['member_level'],"explain"=>$_pop['explain']);
                $this->_Redis->hSetJson('showPlatForm',"showPlatForm",$_data_array,$_exec_time);
                # 判断弹窗是否包含当前时段
                if($_pop['send_time']<=date("Y-m-d H:i:s")){
                    $status=true;
                }
                if($_pop['people_type']==2 and $_member_type==2){
                    $status=true;
                }
                if($_pop['people_type']==3 and !empty($_pop['member_level'])){
                    $_level=explode(',',$_pop['member_level']);
                    if(in_array($_member_level,$_level)){
                        $status=true;
                    }
                }
                if($status){
                    return array("status"=>$status,"data"=>$_data_array);
                }else{
                    return array("status"=>$status);
                }
            }else{
                # 计算结束时间距离现在多少秒
                $_exec_time=strtotime(date("Y-m-d 23:59:59"))-time();
                $this->_Redis->hSetJson('showPlatForm',"showPlatForm",array("pop_status"=>false),$_exec_time);
                return array("status"=>$status);
            }
        }
    }

    /**
     * @return array
     * @context 展示红包信息
     */
    function showRedPacket()
    {
        $redpacket=new RedPacketService($this->memberInfo["code"],$this->memberInfo["level"]);
        return $redpacket->memberRedPacketListInfo();
    }

    /**
     * @return array
     * @context 轮播展示
     */
    function showScroll()
    {
        $scroll=new ScrollborderService();
        return $scroll->scroll_list();
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 收下系统赠送
     */
    function confirmReceived()
    {
        $this->memberInfo['is_show']=2;
        $this->_Redis->hSetJson("memberInfo",$this->memberInfo['code'],$this->memberInfo);
        Db::table("member")->where(array("code"=>$this->memberInfo['code']))->update(array("is_show"=>2));
        return array("status"=>true);
    }



}
